/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bar.carta.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.model.SelectItem;

import com.bar.carta.model.LanguageDTO;
import com.bar.carta.model.Tipo;
import com.bar.carta.model.TipoDTO;
import com.bar.carta.model.TipoIdioma;
import com.bar.carta.service.TipoIdiomaLocalServiceUtil;
import com.bar.carta.service.TipoLocalServiceUtil;
import com.bar.carta.service.base.TipoLocalServiceBaseImpl;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.ORMException;
import com.liferay.portal.kernel.dao.orm.Order;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * The implementation of the tipo local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link com.bar.carta.service.TipoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author pgonzalm
 * @see com.bar.carta.service.base.TipoLocalServiceBaseImpl
 * @see com.bar.carta.service.TipoLocalServiceUtil
 */
public class TipoLocalServiceImpl extends TipoLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link com.bar.carta.service.TipoLocalServiceUtil} to access the tipo local service.
	 */
	
private Log log = LogFactoryUtil.getLog(TipoLocalServiceBaseImpl.class);
	
	public List<Tipo> selectTipos(){
		List<Tipo> tipos = new ArrayList<Tipo>();
		List<Tipo> tiposAux = new ArrayList<Tipo>();
		
		try{
			DynamicQuery dQ = DynamicQueryFactoryUtil.forClass(Tipo.class);			
			Order order = OrderFactoryUtil.asc("tipoId");
			dQ.addOrder(order);
			
			tiposAux = TipoLocalServiceUtil.dynamicQuery(dQ);
			
			if (tiposAux != null && !tiposAux.isEmpty()){
				tipos = selectIdiomas(tiposAux, null);
			}
		
		} catch (ORMException e) {
			log.error("Error get tipos. " + " Error massage: " + e.getMessage());
		} catch (SystemException e) {
			log.error("Error to eject query of get tipos. " + " Error massage: " + e.getMessage());
		}
			
		return tipos;
	}
	
	public List<SelectItem> selectTipos(int idioma){
		List<Tipo> tiposAux = new ArrayList<Tipo>();
		List<SelectItem> tiposMap = new ArrayList<SelectItem>();
		
		try{
			DynamicQuery dQ = DynamicQueryFactoryUtil.forClass(Tipo.class);			
			Order order = OrderFactoryUtil.asc("tipoId");
			dQ.addOrder(order);
			
			tiposAux = TipoLocalServiceUtil.dynamicQuery(dQ);
			
			if (tiposAux != null && !tiposAux.isEmpty()){
				List<Tipo> tipos = selectIdiomas(tiposAux, idioma);
				
				if (tipos != null && !tipos.isEmpty()){
					for (Tipo tipo : tiposAux){
						TipoDTO type = (TipoDTO) tipo;
						if (type.getLanguages().get(idioma) != null){
							tiposMap.add(new SelectItem(idioma, type.getLanguages().get(idioma).getName()));
						}
					}
				}				
			}
			
		} catch (ORMException e) {
			log.error("Error get tipos. " + " Error massage: " + e.getMessage());
		} catch (SystemException e) {
			log.error("Error to eject query of get tipos. " + " Error massage: " + e.getMessage());
		}
		return tiposMap;
	}
	
	private List<Tipo> selectIdiomas(List<Tipo> tiposAux, Integer idioma){
		List<TipoIdioma> idiomas = new ArrayList<TipoIdioma>();
		List<Tipo> tipos = new ArrayList<Tipo>();		
		Collection<Object> tipoIds = new ArrayList<Object>();
		
		for (Tipo tipo : tiposAux){
			tipoIds.add(tipo.getTipoId());
		}
		try{
			DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(TipoIdioma.class);
			dynamicQuery.add(RestrictionsFactoryUtil.in("tipoId", tipoIds));
			if (idioma != null){
				dynamicQuery.add(RestrictionsFactoryUtil.eq("idioma", idioma));
			}
			Order order = OrderFactoryUtil.asc("tipoId");
			dynamicQuery.addOrder(order);
			
			idiomas = TipoIdiomaLocalServiceUtil.dynamicQuery(dynamicQuery);
			
			if (idiomas != null && !idiomas.isEmpty()){
				
				for (Tipo tipoAux : tiposAux){
				
					TipoDTO tipo = new TipoDTO(tipoAux);
					Map<Integer, LanguageDTO> lenguajes = new HashMap<Integer, LanguageDTO>();
					
					for (TipoIdioma idiomaAux : idiomas){
						LanguageDTO lenguaje = new LanguageDTO();
						lenguaje.setName(idiomaAux.getName());
						lenguaje.setDescription(idiomaAux.getDescripcion());
						lenguaje.setLanguage(idiomaAux.getIdioma());
						lenguajes.put(idiomaAux.getIdioma(), lenguaje);
					}
					
					tipo.setLanguages(lenguajes);
					
					tipos.add(tipo);
				}
			}
		} catch (ORMException e) {
			log.error("Error get idiomas. " + " Error massage: " + e.getMessage());
		} catch (SystemException e) {
			log.error("Error to eject query of get idiomas. " + " Error massage: " + e.getMessage());
		}
		return tipos;
	}
	
	public void addUpdateTipo(Tipo tipo, List<TipoIdioma> lenguajes){
		Tipo tipoAux;
		try {
			if (tipo.getTipoId() == 0){
				long tipoId = CounterLocalServiceUtil.increment("Tipo");
				Tipo tipoBase = tipoPersistence.create(tipoId);
				
				tipoBase.setActivo(tipo.isActivo());
				tipoBase.setCreateDate(new Date());
							
				tipoAux = TipoLocalServiceUtil.updateTipo(tipoBase);
			}else{
				tipoAux = TipoLocalServiceUtil.updateTipo(tipo);
			}
			for (TipoIdioma language : lenguajes){
				if (language.getTipoIdiomaId() == 0){
					long tipoIdiomaId = CounterLocalServiceUtil.increment("TipoIdioma");
					TipoIdioma tipoIdiomaBase = tipoIdiomaPersistence.create(tipoIdiomaId);
					
					tipoIdiomaBase.setName(language.getName());
					tipoIdiomaBase.setDescripcion(language.getDescripcion());
					tipoIdiomaBase.setIdioma(language.getIdioma());
					tipoIdiomaBase.setTipoId(tipoAux.getTipoId());
					
					TipoIdiomaLocalServiceUtil.updateTipoIdioma(tipoIdiomaBase);
				}else{
					TipoIdiomaLocalServiceUtil.updateTipoIdioma(language);
				}
			}
			
		}catch (SystemException e) {
			log.error("Error to add a tipo: " + e.getMessage());
		}
	}
}